/*
* @author : Javier Camelis
* @project : http://code.google.com/p/joc-cms
* @version : 0.2.0
*/


(function(window){
  var date = (function (_date , _type , _lang){
    var d = /%d/g,
    dd = /%dd/g ,
    m = /%m/g,
    mm = /%mm/g,
    yy = /%yy/g,
    yyyy = /%yyyy/g,
    M = /%M/g,
    MM = /%MM/g,
    D = /%D/g
    DD = /%DD/g ;
    var date = function(_date , _type , _lang){
      return new date.fn.init(_date , _type , _lang);
    }
    date.fn = date.prototype = {
      constructor: date,
      init: function(_date , _type ,_lang ) {
        this.lang = _lang || this.lang ;
        if(!_date && !_type){
          this._date = new Date()
          return this ;
        }
        if(_date.dateversion){
          //in the future i will aply some stuff here
          this._date = _date._date
          return this
        }
        if(_date.getFullYear){
          this._date = _date
          return this ;
        }
      },
      _date : null,
      _format : '%dd/%mm/%yyyy',
      dateversion : 'v 0.2.0',
      lang_pack : { 'es_ES' : { 
         weekday : ['Domingo','Lunes','Martes','Miercoles','Jueves','Viernes','Sabado'] ,
         month : ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
         weekday_short : ['Dom','Lun','Mar','Mie','Jue','Vie','Sab'] ,
         month_short : ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic']
        }
      },
      lang : 'es_ES'
    }
    date.fn.get = function(_format){
      this._format = _format || this._format
      this._date_copy = this._date
      this._date_copy.setMinutes(-this._date_copy.getTimezoneOffset())
      var day = this._date_copy.getDate() , 
      day_week = this.lang_pack[this.lang].weekday[this._date_copy.getUTCDay()] ,
      day_week_short = this.lang_pack[this.lang].weekday_short[this._date_copy.getUTCDay()] ,
      month = this._date_copy.getMonth() + 1,
      month_name = this.lang_pack[this.lang].month[this._date_copy.getUTCMonth()],
      month_name_short = this.lang_pack[this.lang].month_short[this._date_copy.getUTCMonth()],
      year = this._date_copy.getFullYear() ,
      aux_date = this._format;
      aux_date = aux_date.replace(dd,day < 10 ? '0'+day : day)
      aux_date = aux_date.replace(d,day)
      aux_date = aux_date.replace(DD,day_week)
      aux_date = aux_date.replace(D,day_week_short)
      aux_date = aux_date.replace(mm,month < 10 ? '0'+month : month)
      aux_date = aux_date.replace(m,month)
      aux_date = aux_date.replace(MM,month_name)
      aux_date = aux_date.replace(M,month_name_short)
      aux_date = aux_date.replace(yyyy,year)
      aux_date = aux_date.replace(yy,year.toString().substr(2))
      return aux_date
    }
    date.fn.addDay = function(_number){
      this.add(_number,'day');
      return this
    }
    date.fn.addMonth = function(_number){
      this.add(_number,'month');
      return this
    }
    date.fn.addYear = function(_number){
      this.add(_number,'year');
      return this
    }
    date.fn.add = function(_number,_type){
      _type = _type || 'day'
      switch(_type){
        case 'day' :this._date.setDate(this._date.getDate() + _number);
          break;
        case 'month' :this._date.setMonth(this._date.getMonth() + _number);
          break;
        case 'year' :this._date.setFullYear(this._date.getFullYear() + _number);
          break;
        case 'minutes' :this._date.setMinutes(this._date.getMinutes() + _number);
          break;
      }
      return this
    }
    date.fn.getDate = function (){
      return this._date 
    }
    date.fn.sub = function(_comp,_type,_neg){
      _type = _type || 'day'
      _comp = _comp.getDate().getTime()
      var _result = 0;
      var _this = this.getDate().getTime()
      switch(_type){
        case 'day': _result = parseInt((_comp - _this ) / ( 1000 * 60 * 60 * 24 ) );
          break ;
        case 'week': _result = parseInt((_comp - _this ) / ( 1000 * 60 * 60 * 24 * 7 ) );
          break ;
      }
      if(!_neg){
        _result = _result > 0 ? _result : _result * (-1) ;
      }
      return _result
    }
    date.fn.init.prototype = date.fn;
    
    return date
  })();
  
/* 
 * Defined as jQuery plugin
 * window.jQuery.date = window.date = date ;
 * 
 * Defined just as a simple Object ;
 * window.date = date ;
*/
window.jQuery.date = window.date = date ;

})(window)